namespace com.crownpack.initiatives.Features.WebLevel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;

    using com.crownpack.Exceptions.SharePointExceptions;
    using com.crownpack.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "0f864c35-c0cb-4a1d-9fd8-578a6abf5312" )]
    public class WebLevelEventReceiver : SPFeatureReceiver
    {
        #region Fields

        const string SITE = "i2e";

        static string _Title;

        #endregion Fields

        #region Properties

        public static Guid ID
        {
            get
            {
                return new Guid( "614d5cc7-6968-4e7e-aba7-dad5bd9a73a0" ); // from feature manifest
            }
        }

        public static string Title
        {
            get
            {
                if ( string.IsNullOrEmpty( _Title ) )
                    _Title = SPFarm.Local.GetFeatureDefinition( ID ).GetTitle( );
                return _Title;
            }
        }

        #endregion Properties

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureActivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                if ( !isCorrectSite( properties ) )
                    throw new Exception( string.Format( "The feature cannot be activated in this website. To activate this feature, the website must be a child website of a client site and must be named '{3}'. The specified feature {0} ({1}) was not successfully activated at '{2}'.", ID, Title, properties.GetWebUrl( ), SITE ) );

                using ( InitiativesList_Definition_WebLevel o = new InitiativesList_Definition_WebLevel( properties ) )
                    o.Create( );
            }
            catch ( Exception e )
            {
                throw new FeatureActivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPWeb ) )
                {
                    throw new FeatureDeactivationFailedNotScopedForWebException( ID, Title, properties.GetWebUrl( ) );
                }

                using ( InitiativesList_Definition_WebLevel o = new InitiativesList_Definition_WebLevel( properties ) )
                    o.Delete( );
            }
            catch ( Exception e )
            {
                throw new FeatureDeactivationException( ID, Title, properties.GetWebUrl( ), e );
            }
        }

        bool isCorrectSite( SPFeatureReceiverProperties properties )
        {
            Uri uri = new Uri( properties.GetWebUrl( ) );
            string[ ] s = uri.Segments;
            List<string> k = s.ToList( );
            List<string> l = new List<string>( );
            k.ForEach( a => l.Add( a.ToLower( ).Trim( '/' ) ) );
            return l[ l.Count - 1 ].Equals( SITE, StringComparison.OrdinalIgnoreCase );
        }

        #endregion Methods
    }
}